// 
//  DotNetNuke? - http://www.dotnetnuke.com
//  Copyright (c) 2002-2006
//  by DotNetNuke Corporation
// 
//  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
//  documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
//  the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
//  to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// 
//  The above copyright notice and this permission notice shall be included in all copies or substantial portions 
//  of the Software.
// 
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
//  TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
//  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
//  CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
//  DEALINGS IN THE SOFTWARE.
// 
using System;
using System.Web;
using System.Web.Compilation;
using System.Data;
using System.Reflection;
using System.Collections.Generic;
using DotNetNuke;
using DotNetNuke.Security.Roles;
using DotNetNuke.Security.Membership;
using System.Collections;
using DotNetNuke.Entities.Portals;
using System.Data.SqlClient;
using System.Text;

namespace DotNetNuke.Modules.IWebCSharp
{
    public class IWebCoreData
    {

        #region '"PortalRoles"'

        public static List<IWebRoleInfo> PortalRoles(int PortalId)
        {
            List<IWebRoleInfo> colPortalRoles = new List<IWebRoleInfo>();
            RoleController RoleController = new RoleController();
            ArrayList arrArraylist;
            arrArraylist = RoleController.GetPortalRoles(PortalId);

            IWebRoleInfo tmpIWebRoleInfo = new IWebRoleInfo();
            tmpIWebRoleInfo.RoleName = "Host";
            colPortalRoles.Add(tmpIWebRoleInfo);

            foreach (RoleInfo objRoleInfo in arrArraylist)
            {
                IWebRoleInfo objIWebRoleInfo = new IWebRoleInfo();
                objIWebRoleInfo.RoleName = objRoleInfo.RoleName;
                colPortalRoles.Add(objIWebRoleInfo);
            }

            return colPortalRoles;
        }


        #endregion

        #region '"PortalList"'

        public static List<IWebPortalInfo> PortalList()
        {
            List<IWebPortalInfo> colPortalList = new List<IWebPortalInfo>();
            PortalController colPortalController = new PortalController();
            ArrayList arrArraylist;
            arrArraylist = colPortalController.GetPortals();

            foreach (PortalInfo objPortalInfo in arrArraylist)
            {
                IWebPortalInfo objIWebPortalInfo = new IWebPortalInfo();
                objIWebPortalInfo.PortalID = objPortalInfo.PortalID;
                objIWebPortalInfo.PotalName = "Portal: [" + (objPortalInfo.PortalID.ToString()) + "] " + objPortalInfo.PortalName;
                colPortalList.Add(objIWebPortalInfo);
            }
            return colPortalList;
        }


        #endregion

        #region '"GetMethodsInDatabase"'

        public static List<IWEBMethodInfo> GetMethodsInDatabase(int PorlalID)
        {
            List<IWEBMethodInfo> IWEBMethodInfoList = new List<IWEBMethodInfo>();

            StringBuilder mySqlString = new StringBuilder();
            mySqlString.Append("SELECT PortalID, MethodName, MethodDescription, SecuritySetting, CompanyName, ApplicationName, FirstGrouping, SecondGrouping ");
            mySqlString.Append("FROM {databaseOwner}{objectQualifier}IWebCSharpMethods ");
            mySqlString.Append("WHERE PortalID = @PortalID ");
            mySqlString.Append("ORDER BY PortalID, MethodName ");

            SqlParameter prmPortalID = new SqlParameter("@PortalID", SqlDbType.Int, 4);
            prmPortalID.Value = PorlalID;

            using (IDataReader dr = ((IDataReader)(DotNetNuke.Data.DataProvider.Instance().ExecuteSQL(mySqlString.ToString(), prmPortalID))))
            {
                while (dr.Read())
                {
                    IWEBMethodInfo IWEBMethodInfo = new IWEBMethodInfo();
                    IWEBMethodInfo.MethodName = Convert.ToString(dr["MethodName"]);
                    IWEBMethodInfo.Portal = Convert.ToInt32(dr["PortalID"]);
                    IWEBMethodInfo.MethodDescription = Convert.ToString(dr["MethodDescription"]);
                    IWEBMethodInfo.SecuritySetting = Convert.ToString(dr["SecuritySetting"]);
                    IWEBMethodInfo.CompanyName = Convert.ToString(dr["CompanyName"]);
                    IWEBMethodInfo.ApplicationName = Convert.ToString(dr["ApplicationName"]);
                    IWEBMethodInfo.FirstGrouping = Convert.ToString(dr["FirstGrouping"]);
                    IWEBMethodInfo.SecondGrouping = Convert.ToString(dr["SecondGrouping"]);
                    IWEBMethodInfoList.Add(IWEBMethodInfo);
                }
            }

            return IWEBMethodInfoList;
        }


        #endregion

    }

}

